package com.my.encoder;

import com.my.handler.HttpHandler;
import com.my.util.Threads;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.jboss.netty.handler.codec.http.HttpContentCompressor;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.timeout.IdleStateHandler;

/**
 * @Author hujf
 * @Date 2021/10/25 9:44
 * @Description 解码
 */
public class DetectHttp extends FrameDecoder {

    private HttpHandler httpHandler = new HttpHandler();
    private ChannelHandler timeOutHandler;

    public DetectHttp() {
        timeOutHandler = new IdleStateHandler(Threads.newTimer("tsdb timer"),0,0,100000);
    }

    @Override
    protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer buffer) throws Exception {
        if (buffer.readableBytes() < 1) {
            return null;
        }
        ChannelPipeline pipeline = channelHandlerContext.getPipeline();


        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("inflater", new HttpContentCompressor());
        pipeline.addLast("encoder", new HttpResponseEncoder());

       // pipeline.addLast("timeout",timeOutHandler);
        pipeline.addLast("hander", httpHandler);
        return buffer.readBytes(buffer.readableBytes());
    }

}
